<template>
  <div class="edit-popup">
    <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
      <el-form ref="formRef" :model="formData" label-width="84px">
        <el-form-item label="订单状态" prop="name">
          <el-select v-model="formData.status">
            <el-option v-for="item in optionsData.orderStatus" :key="item.id" :label="item.name" :value="Number(item.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="num">
          <el-input v-model="formData.remark" style="width: 100%" :autosize="{ minRows: 4, maxRows: 6 }"
            type="textarea" />
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import { type FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { detailApi } from '@/api/activity/order'
import { useDictOptions } from '@/hooks/useDictOptions'
import feedback from '@/utils/feedback'
import { computed, reactive, shallowRef } from 'vue'
import { siginApi } from '@/api/activity/order'
import { dictDataLists } from '@/api/setting/dict'
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const popupTitle = computed(() => {
  return '订单状态修改'
})
const { optionsData } = useDictOptions<{
  orderStatus: Array<{
    id: number
    name: string
    value: number
    dictTypeId: number
  }>
}>({
  orderStatus: {
    api: dictDataLists,
    params: { dictType: 'draw_order_status' },
    transformData(data: any) {
      return data.lists
    },
  },
})
const formData = reactive({
  id: '',
  remark: '',
  status: '',
})

const formRules = reactive({})

const handleSubmit = async () => {
  await siginApi(formData)
  popupRef.value?.close()
  feedback.msgSuccess('操作成功')
  emit('success')
}

const open = () => {
  popupRef.value?.open()
}

const setFormData = async (row: any) => {
  const data = await detailApi({
    id: row.id,
  })
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key]
    }
  }
}

const handleClose = () => {
  emit('close')
}

defineExpose({
  open,
  setFormData,
})
</script>
<style lang="scss" scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  text-align: center;
}
</style>
